local U = require "packages/utility/utility"

local shiqi = fk.CreateSkill{
  name = "xiaobai__shiqi",
  tags = {Skill.Limited}
}

Fk:loadTranslationTable{
  ["xiaobai__shiqi"] = "噬契",
  [":xiaobai__shiqi"] = "限定技，一名角色使用装备牌后，你可视为使用【过河拆桥】。"..
    "技能行置期间，其他角色可于其准备阶段视为对你使用【借刀杀人】，你因此造成伤害后，重置本技能。",

  ["$xiaobai__shiqi1"] = "昔邓艾欲杀续,",
  ["$xiaobai__shiqi2"] = "今日当报此恨！"
}

shiqi:addEffect(fk.CardUseFinished, {
  can_trigger =function (self, event, target, player, data)
    return player:hasSkill(self) and data.card.type == Card.TypeEquip and player:usedSkillTimes(shiqi.name, Player.HistoryGame) < 1
  end,
  on_cost = function (self, event, target, player, data)
     local room = player.room
     local use = room:askToUseVirtualCard(player, {
      name = "dismantlement",
      skill_name = shiqi.name,
      skip = true
     })
     if use then
      event:setCostData(self, use)
      return true
     end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local use = event:getCostData(self)
    room:useCard(use)
  end
})

shiqi:addEffect(fk.EventPhaseStart, {
  is_delay_effect = true,
  mute = true,
  anim_type = "special",
  can_trigger = function (self, event, target, player, data)    
    return player:hasSkill(self, true, true) and target ~= player and player:usedSkillTimes(shiqi.name, Player.HistoryGame) > 0 and 
      data.phase == Player.Start
  end,
  on_cost = function (self, event, target, player, data)
    local use = player.room:askToUseVirtualCard(target, {
      skill_name = shiqi.name,
      name = "collateral",
      extra_data = {
        must_targets = {player.id},
        exclusive_targets = {player.id},
      },
      skip = true
    })
    if use then
      event:setCostData(self, use)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local use = event:getCostData(self)  ---@type UseCardData
    room:useCard(use)
    if player.dead then return end
    local rcard = use.cardsResponded
    if rcard then
      if room.logic:getActualDamageEvents(1, function (e)
        if e.data.card == rcard then
          return true
        end
        return false
      end, Player.HistoryPhase) then 
        player:setSkillUseHistory(shiqi.name, 0, Player.HistoryGame)
      end
    end
  end
})

return shiqi